package com.ncucoder.soms.service.impl;

import com.ncucoder.soms.dao.ProviderMapper;
import com.ncucoder.soms.dto.DisplayProviderDTO;
import com.ncucoder.soms.entity.Provider;
import com.ncucoder.soms.service.ProviderService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="https://www.ncucoder.com">hsowan</a>
 * @date 2019-07-22
 **/
@Service
public class ProviderServiceImpl implements ProviderService {

    private final ProviderMapper providerMapper;

    public ProviderServiceImpl(ProviderMapper providerMapper) {
        this.providerMapper = providerMapper;
    }

    @Override
    public List<Provider> listProviders(int page, int pageSize) {
        int beginIndex = pageSize * (page - 1);
        return providerMapper.selectProviders(beginIndex, pageSize);
    }

    @Override
    public int countProviders() {
        return providerMapper.countProviders();
    }

    @Override
    public int deleteProviderById(Long id) {
        return providerMapper.deleteProviderById(id);
    }

    @Override
    public int addProvider(Provider provider) {
        return providerMapper.insertProvider(provider);
    }

    @Override
    public int updateProvider(Provider provider) {
        return providerMapper.updateProvider(provider);
    }

    @Override
    public List<DisplayProviderDTO> listAllProviderNames() {
        List<Provider> providers = providerMapper.listAllProviderNames();
        List<DisplayProviderDTO> displayProviders = new ArrayList<>();
        for (Provider provider: providers) {
            DisplayProviderDTO displayProvider = new DisplayProviderDTO();
            displayProvider.setId(provider.getId());
            displayProvider.setName(provider.getName());
            displayProviders.add(displayProvider);
        }
        return displayProviders;
    }
}
